<?php

namespace App\Listeners;

use App\Events\OrderPaySuccess;
use App\Jobs\SendTempMessage;
use App\Models\MoneyLog;
use App\Models\UserBuyOrder;
use Illuminate\Support\Carbon;

class TgUserOps
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  OrderPaySuccess $event
     * @return void
     */
    public function handle(OrderPaySuccess $event)
    {
        $order = $event->order;
        if ($order->tg_log_id == 0 && $order->tg_money > 0 && $order->tg_user_id > 0) {
            $user = $order->tg_user;
            $money = $order->tg_money;
            switch ($order->item_type) {
                case 'lecture':
                    $type = MoneyLog::TG_LECTURE_GOODS;
                    break;
                case 'channel':
                    $type = MoneyLog::TG_CHANNEL_GOODS;
                    break;
                case 'vip':
                    $type = MoneyLog::TG_VIP_GOODS;
                    break;
            }

            $tg_level = $order->tg_level;//推广分佣等级 （暂不处理，只分1级）

            $log = MoneyLog::addLog($user, $money, $type, '推广收益', UserBuyOrder::class, $order->id);
            if ($log) {
                $order->tg_log_id = $log->id;
                $order->save();
            }

        }
    }
}
